package com.lzh.hospital.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzh.entity.R;
import com.lzh.hospital.service.HospitalService;
import com.lzh.model.Hospital;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author:L枣
 * Date:2024/8/22
 */
@RestController //@RestController = @Controller + @ResponseBody
@RequestMapping("/hospital")
@AllArgsConstructor
@CrossOrigin // 允许跨域访问
public class HospitalController {

    private final HospitalService hospitalService;

    /**
     * 查询所有医院
     * @return
     */
    @GetMapping
    public R findAll() {
        List<Hospital> list = hospitalService.list();
        return new R(list);
    }

    @GetMapping("pageList/{currentPage}/{pageSize}")
    public R pageList(@PathVariable Integer currentPage,@PathVariable Integer pageSize,@RequestBody Hospital hospital) {
        Page<Hospital> page = new Page(currentPage, pageSize);
        return new R(hospitalService.pageList(hospital,page));
    }

    @PutMapping
    public R update(@RequestBody Hospital hospital) {
        return new R(hospitalService.updateById(hospital));
    }

    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id) {
        return new R(hospitalService.removeById(id));
    }

    @PostMapping
    public R save(@RequestBody Hospital hospital) {
        return new R(hospitalService.save(hospital));
    }

}
